﻿namespace KinonekoSoftware.Studio.Models.Modules
{
    public abstract class ElementPresenter<T> : ModulePresenter where T : ModuleElement
    {
        private readonly T _AssociatedObject;

        protected virtual void OnInitialize(T _)
        {
            RaiseUpdated(nameof(Description));
            RaiseUpdated(nameof(Title));
        }

        /// <summary>
        /// 接受外部的喵喵咒语更改。
        /// </summary>
        /// <param name="variant">要同步的内容</param>
        public override void DataSync(Variant variant)
        {
            
        }

        /// <summary>
        /// 帮助方法，用于触发属性变更通知。
        /// </summary>
        /// <param name="oldValue">旧的喵喵咒语</param>
        /// <param name="value">新定的喵喵咒语</param>
        protected void RaiseMetadataChanged(string oldValue, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Queue?.OnNext(new MetadataChangedMSG
                {
                    Block     = TopLevel,
                    Element   = this,
                    IsRemoved = true,
                    OldSpell  = oldValue,
                    NewSpell  = null,
                });
            }
            else
            {
                Queue?.OnNext(new MetadataChangedMSG
                {
                    Block     = TopLevel,
                    Element   = this,
                    IsRemoved = false,
                    OldSpell  = oldValue,
                    NewSpell  = value,
                });
            }
        }

        /// <summary>
        /// 帮助方法，用于触发属性变更通知。
        /// </summary>
        /// <param name="metadata">指定的喵喵咒语</param>
        protected void RaisePropertyUpdated(string metadata)
        {
            if (string.IsNullOrEmpty(metadata))
            {
                return;
            }

            Queue?.OnNext(new ValueChangedMSG
            {
                Block    = TopLevel,
                Element  = this,
                Metadata = metadata,
            });
        }

        /// <summary>
        /// 唯一标识符
        /// </summary>
        public string Id => AssociatedObject?.Id;

        /// <summary>
        /// <see cref="AssociatedObject"/> 表示原始的对象。
        /// </summary>
        public required T AssociatedObject
        {
            get => _AssociatedObject;
            init
            {
                if (value is null)
                {
                    return;
                }

                _AssociatedObject = value;
                OnInitialize(value);
            }
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description
        {
            get => AssociatedObject?.Description;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Description = value;
                RaiseUpdated();
            }
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get => AssociatedObject?.Title ?? TextSR.Parameter_Untitled;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Title = value;
                RaiseUpdated();
            }
        }

        /// <summary>
        /// 包含当前 <see cref="ElementPresenter{T}"/> 的 <see cref="GroupPresenter{T}"/>
        /// </summary>
        public required ModulePresenter TopLevel { get; init; }
    }
}